//package lesson04;
//
//public class Demo_403 {
//      static Object object=new Object();
//      static volatile int nums=0;
//      static volatile int getNums1=0;
//    static volatile int getNums2=0;
//    public static void main(String[] args) throws InterruptedException {
//        Thread  thread1=new Thread(()->{
//                while (getNums1!=500){
//                    synchronized (object) {
//                        System.out.println("我要买包子");
//                        if (nums == 0){
//                            try {
//                                object.wait();
//                            } catch (InterruptedException e) {
//                                throw new RuntimeException(e);
//                            }
//                    }
//                            nums-=10;
//                            getNums1+=10;
//                            System.out.println("1买到包子了"+"10"+"当前"+getNums1);
//                            System.out.println("------------------------------------");
//
//                    }
//
//                }
//        });
//
//        Thread  thread2=new Thread(()->{
//            while (getNums2!=400){
//                synchronized (object){
//                    System.out.println("我要买包子");
//                    if(nums==0) {
//                        try {
//                            object.wait();
//                        } catch (InterruptedException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }
//
//                        nums-=10;
//                        getNums2+=10;
//                        System.out.println("2买到包子了"+"10"+"当前"+getNums2);
//                        System.out.println("------------------------------------");
//
//                }
//
//            }
//        });
//        Thread thread =new Thread(()->{
//            while (getNums1!=500&&getNums2!=400){
//                synchronized (object){
//                    System.out.println("我在做包子");
//                    nums+=20;
////                    try {
////                        Thread.sleep(5000);
////                    } catch (InterruptedException e) {
////                        throw new RuntimeException(e);
////                    }
//                    if(nums>=10) {
//                        System.out.println("包子做好了");
//                        object.notifyAll();
//                    }
//                }
//            }
//        });
//        thread.start();
//        thread1.start();
//        thread2.start();
//        thread.join();
//        thread1.join();
//        thread2.join();
//        System.out.println(getNums1);
//        System.out.println(getNums2);
//
//    }
//}




package lesson04;

public class Demo_403 {
    private static final Object lock = new Object();
    private static volatile int bunStock = 0; // 包子库存
    private static volatile int customer1Purchased = 0; // 顾客1购买数量
    private static volatile int customer2Purchased = 0; // 顾客2购买数量
    private static final int CUSTOMER1_TARGET = 500; // 顾客1目标购买量
    private static final int CUSTOMER2_TARGET = 200; // 顾客2目标购买量

    public static void main(String[] args) throws InterruptedException {
        // 顾客1线程
        Thread customer1 = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    // 检查是否已完成购买目标
                    if (customer1Purchased >= CUSTOMER1_TARGET) {
                        break;
                    }

                    System.out.println("【顾客1】想买包子");

                    // 如果没有包子，就等待
                    while (bunStock < 10) {
                        try {
                            System.out.println("【顾客1】等待包子...");
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }

                    // 购买包子
                    bunStock -= 10;
                    customer1Purchased += 10;
                    System.out.println("【顾客1】买到10个包子，已购买:" + customer1Purchased + "，库存:" + bunStock);
                    System.out.println("------------------------------------");

                    // 通知其他线程
                   // lock.notifyAll();
                }

                // 模拟购买后的处理时间
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            System.out.println("【顾客1】已完成购买目标");
        });

        // 顾客2线程
        Thread customer2 = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    // 检查是否已完成购买目标
                    if (customer2Purchased >= CUSTOMER2_TARGET) {
                        break;
                    }

                    System.out.println("【顾客2】想买包子");

                    // 如果没有包子，就等待
                    while (bunStock < 10) {
                        try {
                            System.out.println("【顾客2】等待包子...");
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }

                    // 购买包子
                    bunStock -= 10;
                    customer2Purchased += 10;
                    System.out.println("【顾客2】买到10个包子，已购买:" + customer2Purchased + "，库存:" + bunStock);
                    System.out.println("------------------------------------");

                    // 通知其他线程
                  //  lock.notifyAll();
                }

                // 模拟购买后的处理时间
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            System.out.println("【顾客2】已完成购买目标");
        });

        // 包子铺线程
        Thread bakery = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    // 检查是否所有顾客都已完成购买
                    if (customer1Purchased >= CUSTOMER1_TARGET &&
                            customer2Purchased >= CUSTOMER2_TARGET) {
                        break;
                    }

                    System.out.println("【包子铺】开始做包子...");

                    // 制作包子
                    bunStock += 10;
                    System.out.println("【包子铺】做好20个包子，当前库存:" + bunStock);

                    // 通知所有等待的顾客
                    lock.notifyAll();
                }

                // 模拟制作包子的时间
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            System.out.println("【包子铺】所有顾客都已完成购买，停止营业");
        });

        // 启动线程
        bakery.start();
        customer1.start();
        customer2.start();

        // 等待所有线程完成
        bakery.join();
        customer1.join();
        customer2.join();

        // 打印最终结果
        System.out.println
                ("\n最终统计:");
        System.out.println("顾客1购买总量: " + customer1Purchased);
        System.out.println("顾客2购买总量: " + customer2Purchased);
        System.out.println("剩余包子库存: " + bunStock);
    }
}